package com.qifenqian.turbot.module.ui.marketing

import android.app.Activity
import android.content.DialogInterface
import android.content.Intent
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.blankj.rxbus.RxBus
import com.qifenqian.turbot.R
import com.qifenqian.turbot.base.BaseActivity
import com.qifenqian.turbot.module.adapter.marketing.SpecialDishRvAdapter
import com.qifenqian.turbot.module.contract.MarketingContract
import com.qifenqian.turbot.module.entity.response.marketing.SpecialDishEntity
import com.qifenqian.turbot.module.presenter.MarketingPresenter
import com.qifenqian.turbot.module.ui.dish.ACTION_SPECIAL_DISH_CHOOSE
import com.qifenqian.turbot.module.ui.dish.DishManageActivity
import com.qifenqian.turbot.module.ui.dish.RX_DISH
import com.qifenqian.turbot.module.ui.dish.TempDishEntity
import com.qifenqian.turbot.util.Lcat
import com.qifenqian.turbot.widget.CommonDialog
import kotlinx.android.synthetic.main.activity_special_dish.*
import kotlinx.android.synthetic.main.dialog_common.*
import kotlinx.android.synthetic.main.item_special_dish.view.*
import org.jetbrains.anko.toast

/**
 * @author H
 * @createDate 2019/1/3 15:12
 * @description 特价菜
 * @see
 * @since
 */
const val SHOW_ON_HOMEPAGE_YES = 0
const val SHOW_ON_HOMEPAGE_NO = 1

const val SPECIAL_DISCOUNT_TYPE1 = 0
const val SPECIAL_DISCOUNT_TYPE2 = 1
const val SPECIAL_DISCOUNT_TYPE3 = 2

class SpecialDishActivity : BaseActivity<MarketingPresenter, MarketingContract.View>(), MarketingContract.View {

    companion object {
        fun startThis(activity: Activity) {
            val intent = Intent(activity, SpecialDishActivity::class.java)
            activity.startActivity(intent)
        }
    }

    override fun bindLayoutId(): Int {
        return R.layout.activity_special_dish
    }

    override fun bindPresenter(): MarketingPresenter? {
        return MarketingPresenter(this, this)
    }

    private var mSpecialDishList: MutableList<SpecialDishEntity.SpecialDish> = mutableListOf()
    private var mSpecialDishRvAdapter: SpecialDishRvAdapter? = null
    //    private var mDiscountType: Int = -1
    private var mShowOnHome: Int = -1
    private var mHasOtherShowOnHome: Boolean = true

    override fun initial() {
        mPresenter?.getSpecialDishes()
        mShowOnHome = SHOW_ON_HOMEPAGE_NO
        mHasOtherShowOnHome = false
        swi_special_dish_show.isChecked = mShowOnHome == SHOW_ON_HOMEPAGE_YES
        mSpecialDishRvAdapter = SpecialDishRvAdapter(mSpecialDishList)
        rv_special_dish.layoutManager = LinearLayoutManager(this)
        rv_special_dish.adapter = mSpecialDishRvAdapter
        mSpecialDishRvAdapter!!.setOnDishNameClickListener(object : SpecialDishRvAdapter.OnDishNameClickListener {
            override fun onDelete(position: Int, view: View) {
                mPresenter?.deleteSpecialDish(mSpecialDishList[position].id, view)
            }

            override fun onEdit(position: Int) {
                // 跳转到选菜界面
                DishManageActivity.startThis(this@SpecialDishActivity, ACTION_SPECIAL_DISH_CHOOSE, position)
            }
        })
        /*swi_special_dish_other_discount.setOnCheckedChangeListener { buttonView, isChecked ->
            if (isChecked) {
                ly_special_dish_other_discount.visibility = View.VISIBLE
                mDiscountType = SPECIAL_DISCOUNT_TYPE1
                v_special_dish_other_discount1.isSelected = true
                v_special_dish_other_discount2.isSelected = false
                v_special_dish_other_discount3.isSelected = false
            } else {
                ly_special_dish_other_discount.visibility = View.GONE
                mDiscountType = -1
            }
        }
        ly_special_dish_other_discount1.setOnClickListener {
            mDiscountType = SPECIAL_DISCOUNT_TYPE1
            v_special_dish_other_discount1.isSelected = true
            v_special_dish_other_discount2.isSelected = false
            v_special_dish_other_discount3.isSelected = false
        }
        ly_special_dish_other_discount2.setOnClickListener {
            mDiscountType = SPECIAL_DISCOUNT_TYPE2
            v_special_dish_other_discount1.isSelected = false
            v_special_dish_other_discount2.isSelected = true
            v_special_dish_other_discount3.isSelected = false
        }
        ly_special_dish_other_discount3.setOnClickListener {
            mDiscountType = SPECIAL_DISCOUNT_TYPE3
            v_special_dish_other_discount1.isSelected = false
            v_special_dish_other_discount2.isSelected = false
            v_special_dish_other_discount3.isSelected = true
        }*/
        swi_special_dish_show.setOnCheckedChangeListener { buttonView, isChecked ->
            if (isChecked) {
                if (mHasOtherShowOnHome) {
                    showConfirmDialog()
                } else {
                    mShowOnHome = SHOW_ON_HOMEPAGE_YES
                }
            } else {
                mShowOnHome = SHOW_ON_HOMEPAGE_NO
            }
        }
        btn_special_dish_preview.setOnClickListener {
            // TODO: 2019/1/14 查看预览效果
        }
        btn_special_dish_next.setOnClickListener {
            Lcat.a("mShowOnHome", mShowOnHome)
            Lcat.a("SpecialDishActivity", mSpecialDishList)
            mPresenter?.saveSpecialDishes(SpecialDishEntity(mShowOnHome, mSpecialDishList))
        }
    }

    private fun showConfirmDialog() {
        val dialog = CommonDialog(this)
        dialog.tv_dialog_common_content.setSpannableText("您已经设 “满减活动” 为首页展示了，是否替换？", "满减活动", null)
        dialog.setTitleText("温馨提示")
                .setCancel("取消", DialogInterface.OnClickListener { dialog, which ->
                    mShowOnHome = SHOW_ON_HOMEPAGE_NO
                    swi_special_dish_show.isChecked = false
                    dialog!!.dismiss()
                })
                .setOk("确定", DialogInterface.OnClickListener { dialog, which ->
                    mShowOnHome = SHOW_ON_HOMEPAGE_YES
                    swi_special_dish_show.isChecked = true
                    dialog!!.dismiss()
                })
                .setCommonCancelable(false)
                .setCommonCanceledOnTouchOutside(false)
                .show()
    }

    override fun onResume() {
        super.onResume()
        RxBus.getDefault().subscribe(this, RX_DISH, object : RxBus.Callback<TempDishEntity>() {
            override fun onEvent(t: TempDishEntity?) {
                val tempDishRvEntity = SpecialDishEntity.SpecialDish(t!!.dishEntity.dishName, t.dishEntity.id,
                        t.dishEntity.isSetSize, t.dishEntity.price, t.newPriceSmall, t.position + 1,
                        mutableListOf(SpecialDishEntity.SpecialDish.Size("小份", t.dishEntity.sizeList[0].price, t.newPriceSmall),
                                SpecialDishEntity.SpecialDish.Size("中份", t.dishEntity.sizeList[1].price, t.newPriceMiddle),
                                SpecialDishEntity.SpecialDish.Size("大份", t.dishEntity.sizeList[2].price, t.newPriceBig)))
                mSpecialDishList[t.position] = tempDishRvEntity
                mSpecialDishRvAdapter!!.notifyItemChanged(t.position)
                Lcat.a("SpecialDishActivity", mSpecialDishList)
            }
        })
    }

    override fun onDestroy() {
        super.onDestroy()
        RxBus.getDefault().unregister(this)
    }

    override fun getSpecialDishesResult(isSuccess: Boolean, code: Int, msg: String?, specialDishEntity: SpecialDishEntity?) {
        if (isSuccess) {
            if (!specialDishEntity?.specialDishList.isNullOrEmpty()) {
                mSpecialDishList.clear()
                mSpecialDishList.addAll(specialDishEntity!!.specialDishList)
                mSpecialDishRvAdapter?.notifyDataSetChanged()
                swi_special_dish_show.isChecked = specialDishEntity.priority != 0
            } else {
                mSpecialDishRvAdapter?.setEmptyView(R.layout.layout_empty, rv_special_dish)
            }
        } else {
            toast(msg!!)
//            ly_special_dish_main.removeAllViews()
//            val view = LayoutInflater.from(this).inflate(R.layout.layout_error, ly_special_dish_main)
//            view.btn_error_retry.setOnClickListener {
//                mPresenter?.getSpecialDishes()
//            }
        }
    }

    override fun saveSpecialDishesResult(isSuccess: Boolean, code: Int, msg: String?) {
        if (isSuccess) {
            finish()
        }
        toast(msg!!)
    }

    override fun deleteSpecialDishResult(isSuccess: Boolean, code: Int, msg: String?, view: View?) {
        if (isSuccess) {
            view!!.tv_item_special_dish_name.text = "选择菜品"
            view.tv_item_special_dish_name.setTextColor(resources.getColor(R.color.gray_666666))
            view.et_item_special_dish_new_price_small.setText("")
            view.tv_item_special_dish_old_price_small.text = ""
            view.et_item_special_dish_new_price_middle.setText("")
            view.tv_item_special_dish_old_price_middle.text = ""
            view.et_item_special_dish_new_price_big.setText("")
            view.tv_item_special_dish_old_price_big.text = ""
            view.tv_item_special_dish_name.isEnabled = true
            view.et_item_special_dish_new_price_small.isEnabled = true
            view.et_item_special_dish_new_price_middle.isEnabled = true
            view.et_item_special_dish_new_price_big.isEnabled = true
        }
        toast(msg!!)
    }
}
